package com.shigen.demo.enums;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.shigen.demo.entity.SensitiveParams;
import java.util.function.Function;

/**
 * @author shigenfu
 * @date 2023/10/29 12:18
 */
public enum SensitiveStrategyEnum {

    // 自定义
    CUSTOMER((params) -> StrUtil.hide(params.getValue(), params.getStart(), params.getEnd())),
    // 固定电话
    FIXED_PHONE((params) -> DesensitizedUtil.fixedPhone(params.getValue())),
    // 地址
    ADDRESS((params) -> DesensitizedUtil.address(params.getValue(), params.getEnd())),
    // 身份证号
    ID_CARD((params) -> DesensitizedUtil.idCardNum(params.getValue(), params.getStart(), params.getEnd())),
    // 银行卡
    BANK_CARD((params) -> DesensitizedUtil.bankCard(params.getValue())),
    // 车牌号
    CAR_LICENSE((params) -> DesensitizedUtil.carLicense(params.getValue())),
    // 中文名
    CHINESE_NAME((params) -> DesensitizedUtil.chineseName(params.getValue())),
    // 密码
    PASSWORD((params) -> DesensitizedUtil.password(params.getValue())),
    // ipv4
    IPV4((params) -> DesensitizedUtil.ipv4(params.getValue())),
    // 手机号
    MOBILE_PHONE((params) -> DesensitizedUtil.mobilePhone(params.getValue())),
    // 电子邮件
    EMAIL((params) -> DesensitizedUtil.email(params.getValue()));


    private final Function<SensitiveParams, String> function;

    SensitiveStrategyEnum(Function<SensitiveParams, String> function) {
        this.function = function;
    }

    public String desensitize(String value, int start, int end) {
        if (start < 0 || end < 0 || start >= end) {
            return value;
        }
        SensitiveParams params = new SensitiveParams(value, start, end);
        return function.apply(params);
    }

    public String desensitize(String value) {
        SensitiveParams params = new SensitiveParams(value, 0, -1);
        return function.apply(params);
    }

}
